package Vistas;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import Modelos.Factura;
import Modelos.FacturaProducto;
import Modelos.Producto;
import Modelos.ProductoDAO;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrmFactura extends javax.swing.JFrame {
	private JLabel LblCodigo;
	private JComboBox CmbCategoria;
	private JButton BtnAgregar;
	private JTextField txtFecha;
	private JButton BtnBuscar;
	private JTextField TxtCedula;
	private JLabel LblCliente;
	private JButton BtnSalir;
	private JButton BtnFacturar;
	private JTable Jtable1;
	private JTextField TxtCantidad;
	private JLabel LblCantidad;
	private JLabel LblCategoria;
	private JTextField TxtFactura;
	private DefaultTableModel Jtable1Model;
	private Vector<FacturaProducto> Detalle;
	private double acm;

	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public String getCedulaRif(){
		return TxtCedula.getText();
	}
	
	public FrmFactura() {
		super();
		initGUI();
		Habilitar(false);
		TxtFactura.setEnabled(false);
		Detalle=new Vector<FacturaProducto>();
		acm=0;
		
	}
	
	public void LlenarCMB(Vector<Producto> arr){
		DefaultComboBoxModel CmbCategoriaModel = new DefaultComboBoxModel();
			/*new DefaultComboBoxModel(
					new String[] { "Item One", "Item Two" });*/
		for(int i=0;i<arr.size();i++){
			CmbCategoriaModel.addElement(arr.elementAt(i).getNombre());
		}
		CmbCategoria = new JComboBox();
		getContentPane().add(CmbCategoria);
		CmbCategoria.setModel(CmbCategoriaModel);
		CmbCategoria.setBounds(141, 111, 193, 21);
		
	}
	
	public void addListener(ActionListener actionlistener){
		BtnAgregar.addActionListener(actionlistener);
		BtnBuscar.addActionListener(actionlistener);
		//BtnEliminar.addActionListener(actionlistener);
		BtnFacturar.addActionListener(actionlistener);
		BtnSalir.addActionListener(actionlistener);
	}
	
	public int getCantidad(){
		return  Integer.valueOf(TxtCantidad.getText());
	}
	
	public void MensajeError(){
		JOptionPane.showMessageDialog(rootPane,"El nventario no da abasto");
	}
	
	public void AgregarCodigoFactura(int i){
		TxtFactura.setText(String.valueOf(i));
	}
	
	public int getCodigoProducto(){
		return (CmbCategoria.getSelectedIndex()+1);
	}
	
	public void Habilitar(boolean habilitar){
		TxtCantidad.setEnabled(habilitar);
		BtnAgregar.setEnabled(habilitar);
		
	}
	
	public Vector<FacturaProducto> getDetalle(){
		return Detalle;
	}
	
	public void OperacionExitosa(){
		JOptionPane.showMessageDialog(rootPane,"Factura Almacenada");
		this.dispose();
	}
	
	public void AgregarColumna(){
		Vector<Producto> productos = new ProductoDAO().ConsultarProductos();
		double monto=0;
		String nombre = "";
		for(int i = 0;i<productos.size();i++){
			if(productos.elementAt(i).getCodigo()==(CmbCategoria.getSelectedIndex()+1)){
				monto=productos.elementAt(i).getMonto();
				nombre=productos.elementAt(i).getNombre();
				break;
			}
		}
		this.Detalle.add(new FacturaProducto(Integer.valueOf(TxtFactura.getText()),Integer.valueOf(CmbCategoria.getSelectedIndex()+1),Integer.valueOf(TxtCantidad.getText()), monto));
		Vector columna= new Vector();
		columna.add(nombre);
		columna.add(TxtCantidad.getText());
		columna.add(monto*Double.valueOf(TxtCantidad.getText()));
		Jtable1Model.addRow(columna);
		acm+=monto*(Double.valueOf(TxtCantidad.getText()));
	}
	
	public Factura getFactura(){
		
		return new Factura(Integer.valueOf(TxtFactura.getText()) ,"11-05-1989", acm,TxtCedula.getText());
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				LblCodigo = new JLabel();
				getContentPane().add(LblCodigo);
				LblCodigo.setText("Numero de Factura");
				LblCodigo.setBounds(24, 79, 118, 14);
			}
			{
				TxtFactura = new JTextField();
				getContentPane().add(TxtFactura);
				TxtFactura.setBounds(140, 76, 122, 21);
			}
			{
				LblCategoria = new JLabel();
				getContentPane().add(LblCategoria);
				LblCategoria.setText("Producto");
				LblCategoria.setBounds(24, 114, 79, 14);
			}
			{
				/*ComboBoxModel CmbCategoriaModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				CmbCategoria = new JComboBox();
				getContentPane().add(CmbCategoria);
				CmbCategoria.setModel(CmbCategoriaModel);
				CmbCategoria.setBounds(141, 111, 122, 21);*/
			}
			{
				LblCantidad = new JLabel();
				getContentPane().add(LblCantidad);
				LblCantidad.setText("Cantidad a Pedir");
				LblCantidad.setBounds(24, 147, 105, 14);
			}
			{
				TxtCantidad = new JTextField();
				getContentPane().add(TxtCantidad);
				TxtCantidad.setBounds(141, 140, 63, 21);
			}
			{
				Jtable1Model = new DefaultTableModel(0,3);
					/*new DefaultTableModel(
							new String[][] { { "One", "Two" }, { "Three", "Four" } },
							new String[] { "Column 1", "Column 2" });*/
				Jtable1 = new JTable();
				getContentPane().add(Jtable1);
				Jtable1.setModel(Jtable1Model);
				Jtable1.setBounds(24, 181, 467, 143);
			}
			{
				BtnAgregar = new JButton();
				getContentPane().add(BtnAgregar);
				BtnAgregar.setText("Agregar");
				BtnAgregar.setBounds(503, 181, 96, 21);
			}
			{
				BtnFacturar = new JButton();
				getContentPane().add(BtnFacturar);
				BtnFacturar.setText("Facturar y Guardar Cambios");
				BtnFacturar.setBounds(24, 352, 216, 21);
			}
			{
				BtnSalir = new JButton();
				getContentPane().add(BtnSalir);
				BtnSalir.setText("Salir");
				BtnSalir.setBounds(440, 359, 69, 21);
			}
			{
				LblCliente = new JLabel();
				getContentPane().add(LblCliente);
				LblCliente.setText("Cedula o RIF");
				LblCliente.setBounds(24, 31, 89, 14);
			}
			{
				TxtCedula = new JTextField();
				getContentPane().add(TxtCedula);
				TxtCedula.setBounds(140, 28, 122, 21);
			}
			{
				BtnBuscar = new JButton();
				getContentPane().add(BtnBuscar);
				BtnBuscar.setText("Buscar");
				BtnBuscar.setBounds(274, 24, 100, 21);
			}
			
			pack();
			this.setSize(618, 425);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
